;
(function() {

  new Vue({
    el: '.businessCreate',
    data: {
      type: [{ name: '公司', code: 'QY' }, { name: '个人' , code: 'PE' }],
      industryData: [],
      selectedIndustry:[],
      selectedIndustryVal:[],
      params: {
        title: null, //主题
        tarcv: null, // 详细描述
        customerName: null, //客户名称
        tax: null, //税人识别号
        name: null, //联系人
        phone: null,
        email: null,
        address: null,
        zipCode: null,
        customerType:'QY'
      },
      channelTypeData: [], //渠道分类
      selectedChannelType:[],
      selectedChannelTypeVal:[],
      channelNameData: [], //渠道名称
      allchannelNameData: [], //z总的渠道名称
      selectedChannelName:[],
      selectedChannelNameVal:[],
      sourceData: [],
      selectedSource:[],
      selectedSourceVal:[],
      validataflag: false,
      isShowChannelName: true,
    },
    mounted: function() {
      this.getIndustryData()
      this.getSourceData()
      this.getChannelTypeData()
      this.getChannelNameData()
    },
    methods: {
      //关闭弹窗
      hidePopValidata() {
        this.validataflag = false
      },
      //验证客户
      validataCustomer() {
        if (!this.params.title) {
          hl.toast('请填写主题')
          return
        }
        if (!this.params.customerName) {
          hl.toast('请选择客户名称')
          return
        }
        if (!this.params.name) {
          hl.toast('请填写联系人')
          return
        }
        // if (!this.params.phone) {
        //   hl.toast('请填写联系电话')
        //   return
        // }
        if (this.params.email) {
          var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
          if (!reg.test(this.params.email)) {
            hl.toast('邮箱格式不正确')
            this.params.email = '';
            return
          }
        }

        hlhttp.get({
          url: 'customer/check_bizoppt_repeat',
          data: {
            sj_id: '#',
            customer_name: this.params.customerName,
            cstmr_contact: this.params.name,
            cstmr_contact_no: this.params.phone,
            customer_type: this.params.customerType
          },
          success: (res) => {
            if (res.repeat == 'N') this.addBusiness()
            if (res.repeat == 'Y') this.validataflag = true
          }
        })
      },
      getIndustryData () {
        var self = this
        hlhttp.get({
          url: 'configure/industry/dict',
          success: data => {
            self.industryData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      selectedCustomerType(val) {
        this.params.customerType = val;
      },
      getChannelTypeData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH',config_name: 'ZS_CHANNEL_CATE'},
          success: data => {
            let arr = []
            data.map(item =>{
              if(item.status == 'Y'){
                arr.push(item)
              }
            })
            self.channelTypeData = hlArrayUtil.replaceData(arr, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      getChannelNameData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_NAME' },
          success: data => {
            self.allchannelNameData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      getSourceData() {
        var self = this

        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH0SHJI', config_name: 'SJ_SOURCE' },
          success: data => {
            self.sourceData = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }])
          }
        })
      },
      handleSourceChange(data){
        this.selectedSourceVal = data[0]['name'].split()
      },
      handleIndustryChange(data){
        this.selectedIndustryVal = data[0]['name'].split()
      },
      handleChannelTypeChange(data){
        this.channelNameData = []
        this.allchannelNameData.map(item =>{
          if(item.parentDictCode == data[0].dictCode){
            this.channelNameData.push(item)
          }
        })
        this.selectedChannelName = []
        this.selectedChannelTypeVal = data[0]['name'].split()
      },
      handleChannelNameChange(data){
        this.selectedChannelNameVal = data[0]['name'].split()
      },
      hideChannelName() {
        this.isShowChannelName = false
      },
      //新建
      addBusiness() {
        const themeInfo = {
          "theme": this.params.title,
          "source_code": this.selectedSource.length ? this.selectedSource.join() : '#',
          "source_value": this.selectedSourceVal.length ? this.selectedSourceVal.join() : '#',
          "channel_category_code": this.selectedChannelType.length ? this.selectedChannelType.join() : '#',
          "channel_category_value": this.selectedChannelTypeVal.length ? this.selectedChannelTypeVal.join() : '#',
          "channel_name_code": this.selectedChannelName.length ? this.selectedChannelName.join() : '#',
          "channel_name_value": this.selectedChannelNameVal.length ? this.selectedChannelNameVal.join() : '#',
          "description": this.params.tarcv || '#'
        }

        const zlfInfo = {
          "customer_name": this.params.customerName || '#',
          "customer_type": this.params.customerType || '#',
          "idcode": this.params.tax || '#',
          "industry_code": this.selectedIndustry.length ? this.selectedIndustry.join() : '#',
          "industry_value": this.selectedIndustryVal.length ? this.selectedIndustryVal.join() : '#',
          "lperson": this.params.name || '#',
          "customer_addr": this.params.address || '#',
          "phone": this.params.phone || '',
          "email": this.params.email || '#',
          "zipcode": this.params.zipCode || '#'
        }

        // var data = {
        //   theme_info: {
        //     "theme": this.params.title,
        //     "source_code": this.sourceCode,
        //     "source_value": this.sourceCodeKey,
        //     "channel_category_code": this.channerCode,
        //     "channel_category_value": this.channerCodeKey,
        //     "channel_name_code": this.channerNameCodeKey || '#',
        //     "channel_name_value": this.channelNameIptVal || '#',
        //     "description": this.params.tarcv
        //   },
        //   zlf_info: {
        //     "customer_name": this.params.customer,
        //     "customer_type": this.typeAct,
        //     "idcode": this.params.tax,
        //     "industry_code": this.industryAct,
        //     "industry_value": this.industryName,
        //     "lperson": this.params.name,
        //     "customer_addr": this.params.address,
        //     "phone": this.params.phone,
        //     "email": this.params.email,
        //     "zipcode": this.params.zipCode
        //   }
        // }

        var is_myself = 'Y'
        if (hlUrlUtil.GetQueryString('page') == 'businessMange') {
          is_myself = 'N'
        }

        hlhttp.post({
          url: 'customer/biz_oppt',
          data: {
            theme_info: JSON.stringify(themeInfo),
            zlf_info: JSON.stringify(zlfInfo),
            is_myself: is_myself
          },
          success: (res) => {
            hl.toast('新建成功')
            hlUrlUtil.goTo('businessDetails', {
              businessId: res.sjId,
              ddtab: true
            })
          }
        })
      }
    }


  })
}())
